import createAxios from '/@/utils/axios'

/**
 * 角色管理模块API
 */
export default {
    /**
     * 查询角色列表
     * @param params 查询参数
     */
    getRoleList: (params: Record<string, any> = {}) => {
        return createAxios(
            {
                url: '/api/Sys/Role/QueryList',
                method: 'post',
                data: {
                    pageSize: params.limit || 100,
                    pageIndex: params.page || 1,
                    roleName: params.roleName || '',
                    orderBy: params.orderBy || 'id',
                    direction: params.order === 'descending' ? 'desc' : 'asc'
                }
            },
            {
                showSuccessMessage: false
            }
        )
    },

    /**
     * 新增/编辑角色
     * @param data 角色数据
     */
    saveRole: (data: Record<string, any>) => {
        return createAxios(
            {
                url: '/api/Sys/Role/SaveRole',
                method: 'post',
                data
            },
            {
                showSuccessMessage: true
            }
        )
    },

    /**
     * 删除角色
     * @param id 角色ID
     */
    deleteRole: (id: number) => {
        return createAxios(
            {
                url: '/api/Sys/Role/DelRole',
                method: 'post',
                data: {
                    id
                }
            },
            {
                showSuccessMessage: true
            }
        )
    },

    /**
     * 获取用户角色
     * @param userId 用户ID
     */
    getUserRoles: (userId: number) => {
        return createAxios(
            {
                url: '/api/Sys/Role/GetUserRoles',
                method: 'post',
                data: {
                    userId
                }
            },
            {
                showSuccessMessage: false
            }
        )
    },

    /**
     * 分配用户角色
     * @param userId 用户ID
     * @param roleIds 角色ID数组
     */
    assignUserRoles: (userId: number, roleIds: number[]) => {
        return createAxios(
            {
                url: '/api/Sys/Role/AssignUserRoles',
                method: 'post',
                data: {
                    userId,
                    roleIds
                }
            },
            {
                showSuccessMessage: true
            }
        )
    }
}
